﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using SMSROUTER.Business;
using System.Web.UI.HtmlControls;
using Utils;
using System.IO;
using Settings;
using System.Data;
namespace SMSROUTER.UI
{
    public partial class CooperateProviderManagement : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        private void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindGridData();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindDropDownData();
                BindGridData();
            }
        }

        private void BindGridData()
        {
            try
            {
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRecord = 0;
                grvData.DataSource = CooperateProvider.Paging(DB, currentPage, pageSize, ref totalRecord, txtSearchCooperateName.Text.Trim(), txtSearchCooperateShortName.Text.Trim());
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRecord;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng: " + totalRecord;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        private void BindDropDownData()
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindGridData();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindGridData();
            }
            catch { }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                hdfID.Value = ID.ToString();
                switch (e.CommandName)
                {
                    case "Editing":
                        hdfAction.Value = "Editing";
                        CooperateProvider editObj = DB.CooperateProviders.FirstOrDefault(p => p.CooperateID == ID);
                        if (editObj != null)
                        {
                            txtCooperateName.Text = editObj.CooperateName;
                            txtCooperateShortName.Text = editObj.CooperateShortName;
                            txtMOPublicKey.Text = editObj.MOPublicKey;
                            txtMOPublicPrivateKey.Text = editObj.MOPublicPrivateKey;
                            txtMTPublicKey.Text = editObj.MTPublicKey;
                            txtMTPublicPrivateKey.Text = editObj.MTPublicPrivateKey;
                            txtIPPermited.Text = editObj.IPPermited;
                            txtIPBanned.Text = editObj.IPBanned;
                            txtMaxMsgPerDay.Text = (editObj.MaxMsgPerDay ?? (object)"").ToString();
                            chkEnabled.Checked = editObj.Enabled ?? false;
                            chkPublicReleased.Checked = editObj.PublicReleased ?? false;
                            txtBatchSize.Text = (editObj.BatchSize ?? 0).ToString();
                            RegisterStartUpScript("OpenDialog('dialog1',600);");
                        }
                        break;
                    case "Deleting":
                        Authorize(Role.Write);
                        CooperateProvider deleteObj = DB.CooperateProviders.FirstOrDefault(p => p.CooperateID == ID);
                        if (deleteObj != null)
                        {
                            DB.CooperateProviders.DeleteOnSubmit(deleteObj);
                            DB.SubmitChanges();
                            InsertChangeLog("Xóa dữ liệu CooperateProvider");
                            BindGridData();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                if (ValidateInfo())
                {
                    CooperateProvider obj;
                    if (hdfAction.Value == "Editing")
                        obj = DB.CooperateProviders.FirstOrDefault(p => p.CooperateID == int.Parse(hdfID.Value));
                    else
                    {
                        obj = new CooperateProvider();
                    }
                    if (obj != null)
                    {
                        //Gán thuộc tính
                        obj.CooperateName = txtCooperateName.Text.Trim();
                        obj.CooperateShortName = txtCooperateShortName.Text.Trim();
                        obj.MOPublicKey = txtMOPublicKey.Text.Trim();
                        obj.MOPublicPrivateKey = txtMOPublicPrivateKey.Text.Trim();
                        obj.MTPublicKey = txtMTPublicKey.Text.Trim();
                        obj.MTPublicPrivateKey = txtMTPublicPrivateKey.Text.Trim();
                        obj.IPPermited = txtIPPermited.Text.Trim();
                        obj.IPBanned = txtIPBanned.Text.Trim();
                        obj.MaxMsgPerDay = string.IsNullOrWhiteSpace(txtMaxMsgPerDay.Text) ? (int?)null : int.Parse(txtMaxMsgPerDay.Text);
                        obj.Enabled = chkEnabled.Checked;
                        obj.PublicReleased = chkPublicReleased.Checked;
                        obj.BatchSize = string.IsNullOrWhiteSpace(txtBatchSize.Text) ? (int?)null : int.Parse(txtBatchSize.Text);
                        if (hdfAction.Value == "AddNew")
                        {
                            DB.CooperateProviders.InsertOnSubmit(obj);
                        }
                        //save
                        DB.SubmitChanges();
                        //Ghi log tương tác
                        InsertChangeLog(hdfAction.Value == "Editing" ? string.Format("Cập nhật dữ liệu CooperateProvider, CooperateProviderID: {0}", obj.CooperateID) : string.Format("Thêm mới dữ liệu CooperateProvider, CooperateProviderID: {0}", obj.CooperateID));
                    }
                }
                BindGridData();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
    }
}